package jp.ac.u_tokyo.dch.action.csv;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.dao.data.DataDao;
import jp.ac.u_tokyo.dch.dao.data.DataDetailDao;
import jp.ac.u_tokyo.dch.dao.schema.DataSchemaDetailDao;
import jp.ac.u_tokyo.dch.model.db.Data;
import jp.ac.u_tokyo.dch.model.db.DataDetail;
import jp.ac.u_tokyo.dch.model.db.DataItem;
import jp.ac.u_tokyo.dch.model.db.DataSchemaDetail;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.util.DchCommonUtil;
import jp.ac.u_tokyo.dch.view.Constants;

import org.apache.commons.lang.xwork.StringUtils;

import au.com.bytecode.opencsv.CSVWriter;

import com.opensymphony.xwork2.ActionSupport;

public class CsvDownloadAction extends ActionSupport implements DaoServiceAware {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String contentType;
	private Long contentLength;
	private String contentDisposition;
	private String inputName;
	private String bufferSize;
	private InputStream inputStream;

	private String downloadDataSort;
	private String downloadDataSchema;
	private String downloadDataOyaid;

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;



	public String download() throws Exception {

		DataDao dataDao = new DataDao(dbConnection);
		DataDetailDao dataDetailDao = new DataDetailDao(dbConnection);
		DataSchemaDetailDao dataSchemaDetailDao = new DataSchemaDetailDao(dbConnection);

		String csvFileName = getSortName(downloadDataSort) + "_" + DchCommonUtil.getCurrentDate("yyyyMMddHHmm")+".csv";

		StringWriter stringWriter = new StringWriter();

		CSVWriter csvWriter = new CSVWriter(stringWriter);


		String[] schemaInfo = {downloadDataSchema};
		String[] dataoyaid = {downloadDataOyaid};

		csvWriter.writeNext(schemaInfo);
		csvWriter.writeNext(dataoyaid);

		Map<String, Integer> map = new LinkedHashMap<String, Integer>();

		List<DataSchemaDetail> dataSchemaDetails = dataSchemaDetailDao.selectList(downloadDataSchema);

		int i = 0;

		for (DataSchemaDetail detail : dataSchemaDetails) {

			map.put(detail.getFieldKey(), i);
			i++;
		}

		List<String> csvHeaderData = new ArrayList<String>();
		List<String> csvHeaderDataName = new ArrayList<String>();

		for (DataSchemaDetail detail : dataSchemaDetails) {

			csvHeaderData.add(detail.getFieldKey());
			csvHeaderDataName.add(detail.getFieldItemName());
		}

		csvWriter.writeNext(csvHeaderData.toArray(new String[0]));
		csvWriter.writeNext(csvHeaderDataName.toArray(new String[0]));

//		List<Data> downloadDataList = dataDao.selectChildData(downloadDataOyaid, downloadDataSort, downloadDataSchema);
//
//		for (Data downloadData : downloadDataList) {
//
//			List<String> csvData = makeList(i);
//
//			List<DataDetail> details = dataDetailDao.selectByIdForDownLoad(downloadData.getId());
//
//			for (DataDetail detail : details) {
//
//				int index = map.get(detail.getFieldKey());
//
//				List<String> datalist = new ArrayList<String>();
//
//				if (!Constants.DATA_TYPE_TEXTAREA.equals(detail.getDataType())) {
//
//					for (DataItem dataItem : detail.getList()) {
//
//						datalist.add(dataItem.getDbData());
//					}
//				} else {
//					for (DataItem dataItem : detail.getList()) {
//
//						datalist.add(dataItem.getDbAreaData());
//					}
//				}
//
//				csvData.set(index, StringUtils.join(datalist, Constants.DATA_SEPARATOR));
//			}
//
//			csvWriter.writeNext(csvData.toArray(new String[0]));
//		}
		csvWriter.flush();
		// 入力ストリームを定義
		setInputStream(new ByteArrayInputStream(stringWriter.getBuffer().toString().getBytes("UTF-8")));
		// ダウンロード時のファイル名を定義
		setContentDisposition("filename=\"" + csvFileName + "\"");

		// ダウンロード処理を実行
		return SUCCESS;
	}

	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;

	}
	/**
	 * 分類セット
	 * @param sort
	 * @return 分類
	 */
	public String getSortName(String sort) {

		if (Constants.LV4_SORT.equals(sort)) {
			return "subfonds";
		}
		if (Constants.LV5_SORT.equals(sort)) {
			return "series";
		}
		if (Constants.LV6_SORT.equals(sort)) {
			return "item";
		}
		return "";
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public Long getContentLength() {
		return contentLength;
	}

	public void setContentLength(Long contentLength) {
		this.contentLength = contentLength;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public String getInputName() {
		return inputName;
	}

	public void setInputName(String inputName) {
		this.inputName = inputName;
	}

	public String getBufferSize() {
		return bufferSize;
	}

	public void setBufferSize(String bufferSize) {
		this.bufferSize = bufferSize;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public DBConnection getDbConnection() {
		return dbConnection;
	}

	public void setDbConnection(DBConnection dbConnection) {
		this.dbConnection = dbConnection;
	}

	public String getDownloadDataSort() {
		return downloadDataSort;
	}

	public void setDownloadDataSort(String downloadDataSort) {
		this.downloadDataSort = downloadDataSort;
	}

	public String getDownloadDataSchema() {
		return downloadDataSchema;
	}

	public void setDownloadDataSchema(String downloadDataSchema) {
		this.downloadDataSchema = downloadDataSchema;
	}

	public String getDownloadDataOyaid() {
		return downloadDataOyaid;
	}

	public void setDownloadDataOyaid(String downloadDataOyaid) {
		this.downloadDataOyaid = downloadDataOyaid;
	}
	public List<String> makeList(int i) {

		List<String> list = new ArrayList<String>();

		for (int k = 0; k < i; k++) {
			list.add("");
		}

		return list;
	}


}
